package cn.yx.common.security.webflux.handler;

import cn.yx.common.core.pojo.ResponseVo;
import cn.yx.common.webflux.util.WebFluxUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * <p>未认证处理器</p>
 *
 * @author Wgssmart
 */
@Component
public class CustomAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {

    @Override
    public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException ex) {
        return Mono.defer(() -> Mono.just(exchange.getResponse())).flatMap(response -> WebFluxUtil.write(response, ResponseVo.authenticationError()));
    }

}